package com.srunpass.algorithm;

/**
 * @author srunpass
 * @version Id: 数字1的个数, v 0.1 2024/7/2 13:06 srunpass Exp $
 */
public class 数字1的个数 {
    public static void main(String[] args) {
        数字1的个数 ss = new 数字1的个数();

        System.out.println(ss.countDigitOne(824883294));
    }
    public int countDigitOne(int n) {
        return (int)gene(n,9);
    }

    long gene(int n, long index) {
        if(index==-1){
            return 0;
        }
        int num = 1;
        for (int i = 0; i < index; i++) {
            num *= 10;
        }
        int first = n/num;
        if(first==0){
            return gene(n,index-1);
        }else if(first==1){
            return index*num/10+n%num+1+gene(n%num,index-1);
        }else {
            return first*index*num/10+num+gene(n%num,index-1);
        }
    }

}
